package 多线程篇;/*
 *author Administrator
 *date 2021年6月2日
 */

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

//        for (int i = 0; i < 50; i++) {
//
//        }

        // 继承 thread
        ThreadDemoRun ThreadDemoRun1 = new ThreadDemoRun("ThreadDemoRun1     ");
        ThreadDemoRun ThreadDemoRun2 = new ThreadDemoRun("ThreadDemoRun2     ");
        ThreadDemoRun1.start();

        // 实现runnable
        Thread runnable = new Thread(new RunnableDemoRun("runnable"));
        runnable.start();

        // 实现callable
        CallableDemoRun<String> callable = new CallableDemoRun<>("callable");
        FutureTask<String> task = new FutureTask<>(callable);
        Thread thread = new Thread(task);
        thread.start();
        String s = task.get();
        System.out.println(s);
        //ThreadDemoRun1.start();
        //ThreadDemoRun2.run();

    }


}
// 创建线程的三种方式
// 1 继承thread
class ThreadDemoRun extends Thread {
    private String name ;

    public ThreadDemoRun(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(name + " 我是线程: "+Thread.currentThread().getName());
    }
}

// 2 实现runnable
class RunnableDemoRun implements Runnable{

    private String name ;

    public RunnableDemoRun(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(name + " 我是线程: "+Thread.currentThread().getName());
    }
}

// 3 实现Callable
class CallableDemoRun<T> implements Callable<T> {
    private String name ;

    public CallableDemoRun(T name) {
        this.name =(String) name;
    }

    @Override
    public T call() throws Exception {
        System.out.println(name + " 我是线程: "+Thread.currentThread().getName());
        return (T) "Hello Callable";
    }
}
